package com.jwb.pub.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.dao.BaseDAO;
import com.jwb.pub.service.RoleService;
import com.jwb.util.MainController;
import com.jwb.util.ToolsUtil;
@Slf4j
@Controller
@RequestMapping("/pub/role")
public class RoleController extends MainController{

	@Autowired
	private BaseDAO baseDAO;
	
	@Autowired
	private RoleService roleService;
	
	/**
	 * 
	 * @description：显示角色列表
	 * @author LIU CHANG
	 * 
	 * 
	 * @createDate 2018-03-19
	 * @createTime 下午3:43:02
	 */	
	
	@RequestMapping("/getRoleList")
	@ResponseBody
	public String getRoleList()
	{
		JSONObject jsdata = new JSONObject();
		JSONObject jsobject = new JSONObject();
		jsobject = roleService.getRoleList(jsdata);
		log.info("getRoleList():" + jsobject);
		return jsobject.toString();
	}
	
	
	/**
	 * 
	 * @description：保存角色信息
	 * @author LIU CHANG
	 * 
	 * 
	 * @createDate 2018-03-19
	 * @createTime 下午3:35:16
	 * http://127.0.0.1:8080/ssm/pub/role/getRoleList
	 */

	@RequestMapping("/roleInfoSave")
	@ResponseBody
	public String roleInfoSave(HttpServletRequest req){
		JSONObject jsdata = new JSONObject();
		jsdata.put("roleName",req.getParameter("roleName"));
		JSONObject jsobject = new JSONObject();
		jsobject = roleService.roleInfoSave(jsdata);		
		log.info("roleInfoSave()"+jsobject);
		return jsobject.toString();
	}

	
	
	/**
	 * 角色权限对应关系维护
	 * @description：
	 * @author LIU CHANG
	 *
	 *
	 * @createDate 2018-3-19
	 * @createTime 下午4:28:40
	 * http://127.0.0.1:8080/ssm/pub/role/roleEdit?roleId=1
	 */
	
	@RequestMapping("/roleEdit")
	@ResponseBody
	public String roleEdit(HttpServletRequest req){
		JSONObject jsdata = new JSONObject();
		jsdata.put("roleId", req.getParameter("roleId"));
		JSONObject jsobject = new JSONObject();
		jsobject = roleService.roleEdit(jsdata);
		log.info("roleEdit()"+jsobject);
		return jsobject.toString();
	}
	
	
	
	
	
	/**
	 * 保存角色权限信息
	 * 
	 * @author LIU CHANG
	 * 
	 * @return
	 * 
	 */
	
	@RequestMapping("/rolePrivSave2")
	@ResponseBody
	public void rolePrivSave2()
	{
		//roleSave(role);
//		String addSql = "insert into role(role_id,role_name) values('"+roleId+"','"+roleName+"')";
//		pubDAO.excuteSql(addSql);
		
//		boolean flg = false;
//		
//		flg = roleService.roleSave(role);
//		
//		RolePriv rp = new RolePriv();
//		RolePrivId rpi = new RolePrivId();
//		
//		rpi.setRole(role);
//		rp.setId(rpi);
//		if (ckprivcode != null)
//		{
//			flg = roleService.rolePrivSave(rp, role, ckprivcode);
//			
//			if (flg)
//			{
//				msgInfo = "角色所对应的权限设置成功!";
//				msgType = "1";
//				hrefAction = "roleList.action";
//			}
//			else
//			{
//				msgInfo = "角色所对应的权限设置失败!";
//				msgType = "2";
//				hrefAction = "roleList.action";
//			}
//		}
//		else
//		{
//			msgInfo = "角色所对应的权限不能为空!";
//			msgType = "2";
//		}
	}
	/**
	 * 
	 * @param req
	 * @return
	 * http://127.0.0.1:8080/ssm/pub/role/getPrivCodes?privList=
	 */
	@RequestMapping("/getPrivCodes")
	@ResponseBody
	public String getPrivCodes(HttpServletRequest req){
		JSONObject jsobject = new JSONObject();
		JSONObject jsdata = new JSONObject();
		jsdata.put("privList", req.getParameter("privList")==null?"":req.getParameter("privList"));
		jsobject = roleService.getPrivCodes(jsdata);
		log.info("getPrivCodes()"+jsobject);
		return jsobject.toString();
	}
	
	
	
	/**
	 * 
	 * @description：
	 * @author LIU CHANG
	 *
	 *
	 * @createDate 2018-3-19
	 * @createTime 下午4:36:32
	 * http://127.0.0.1:8080/ssm/pub/role/rolePrivSave?roleId=2&roleName=test&privCodes=1,2,3
	 */
	
	@RequestMapping("/rolePrivSave")
	@ResponseBody
	public String rolePrivSave(HttpServletRequest req){
		JSONObject jsobject = new JSONObject();
		JSONObject jsdata = new JSONObject();
		jsdata.put("roleId", req.getParameter("roleId")==null?"":req.getParameter("roleId"));
		jsdata.put("privCodes", req.getParameter("privCodes")==null?"":req.getParameter("privCodes"));
		jsdata.put("roleName", req.getParameter("roleName")==null?"":req.getParameter("roleName"));
		jsobject =roleService.rolePrivSave(jsdata);		
		log.info("rolePrivSave()"+jsobject);
		return jsobject.toString();
	}
	
	
	
	/**
	 * 
	 * @description：删除角色信息
	 * @author LIU CHANG
	 * 
	 * 
	 * @createDate 2018-3-19
	 * @createTime 下午3:35:27
	 * http://127.0.0.1:8080/ssm/pub/role/roleDelete?roleId=2
	 */
	@RequestMapping("/roleDelete")
	@ResponseBody
	public String roleDelete(HttpServletRequest req)
	{
		JSONObject jsobject = new JSONObject();
		JSONObject jsdata = new JSONObject();
		jsdata.put("roleId", req.getParameter("roleId")==null?"":req.getParameter("roleId"));		
		jsobject = roleService.roleDelete(jsdata);
		log.info("roleDelete()"+jsobject);
		return jsobject.toString();
	}
	
	
	
}
